﻿using GalaSoft.MvvmLight.CommandWpf;
using HuiZe.education.Common;
using HuiZe.education.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace HuiZe.education.ViewModel
{
    public class SelectChapterViewModel : ViewModelBase
    {
        private ObservableCollection<TreeModel> treenodes = new ObservableCollection<TreeModel>();
        public ObservableCollection<TreeModel> TreeNodes
        {
            get { return treenodes; }
            set
            {
                treenodes = value;
                Set("TreeNodes", ref treenodes, value);
            }
        }
        public SelectChapterViewModel()
        {
            GetzjSourceList();
        }


        /// <summary>
        /// 获取章节类表
        /// </summary>
        public void GetzjSourceList()
        {
            ReqCourseSourceModel model = new ReqCourseSourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseResourceMainModel>>("/api/resources/getKindList", data, "获取资源列表");
            if (result.Item1)
            {
                treenodes.Clear();
                var resources = result.Item2.data;
                foreach (var res in resources)
                {
                    TreeModel treemodel = new TreeModel
                    {
                        NodeID = res.id,
                        NodeName = res.kind_name,

                    };
                    List<TreeModel> TreeList = new List<TreeModel>();
                    foreach (var item in res.kind_item)
                    {
                        TreeModel childtree = new TreeModel
                        {
                            NodeID = item.id,
                            NodeName = item.kind_item_name,
                            ParentID = res.id,
                        };
                        TreeList.Add(childtree);

                    }
                    treemodel.ChildNodes = new ObservableCollection<TreeModel>(TreeList);
                    treenodes.Add(treemodel);
                }
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }
        }


        /// <summary>
        /// 获取章节列表
        /// </summary>
        public void GetzjDirList()
        {

            ReqCourseDirModel model = new ReqCourseDirModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                item_id = ""
            };


            string data = JsonHelper.ToJson(model);
            var result = Request<resCourseDirMainModel>("/api/resources/getDirList", data, "获取资源列表");
            if (result.Item1)
            {

            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }

        }
        /// <summary>
        /// 获取章节标签
        /// </summary>
        public List<resCourseTagChildModel> GetzjTagsList()
        {

            ReqCourseSourceModel model = new ReqCourseSourceModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
            };


            string data = JsonHelper.ToJson(model);
            var result = Request<List<resCourseTagChildModel>>("/api/resources/getTagsList", data, "获取资源列表");
            if (result.Item1)
            {
                return result.Item2.data;
            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
                return null;
            }

        }

        /// <summary>
        /// 获取章节课件
        /// </summary>
        public void GetzjResourcesCourseList()
        {

            ReqCourseSourceListModel model = new ReqCourseSourceListModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                id = 0,
                tags = 0,
            };


            string data = JsonHelper.ToJson(model);
            var result = Request<resCourseListModel>("/api/resources/getResourcesList", data, "获取资源列表");
            if (result.Item1)
            {

            }
            else
            {
                if (result.Item2 != null)
                    ShowDialogView(result.Item2.msg);
            }

        }

    }
}
